package com.example.springboot.controller;


import com.example.springboot.utils.MD5Utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
public class Test {
    //    public static void main(String[] args) {
//        String msg = "22.122";
//        String pattern = "^\\d+(\\.\\d{2})?$";
//        boolean matches = msg.matches(pattern);
//        if (matches){
//            log.info("结果是："+matches);
//        }else {
//            log.info("结果是："+matches);
//        }
//
//    }
//    public static void main(String[] args) {
//        // 获取当前日期和时间
//        LocalDateTime currentDateTime = LocalDateTime.now();
//
//        // 获取当前日期
//        LocalDate currentDate = currentDateTime.toLocalDate();
//
//        // 获取次日凌晨时间
//        LocalDateTime nextDayMidnight = LocalDateTime.of(currentDate.plusDays(1), LocalTime.MIDNIGHT);
//
//        System.out.println("次日凌晨时间：" + nextDayMidnight);
//    }
//    public static void main(String[] args) {
//        // 获取当前日期和时间
//        LocalDateTime currentDateTime = LocalDateTime.now();
//
//        // 获取当前日期
//        LocalDate currentDate = currentDateTime.toLocalDate();
//
//        // 获取次日凌晨时间
//        LocalDateTime nextDayMidnight = LocalDateTime.of(currentDate.plusDays(1), LocalTime.MIDNIGHT);
//
//        // 格式化次日凌晨时间为字符串
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
//        String nextDayMidnightString = nextDayMidnight.format(formatter);
//
//        System.out.println("次日凌晨时间：" + nextDayMidnightString);
//    }


    public void text(String[] args) {
        Map<String, String> provinceMap = new HashMap<>();
        Map<String, String> cityMap = new HashMap<>();
        Map<String, String> countyMap = new HashMap<>();
        provinceMap.put("11", "北京");
        provinceMap.put("12", "天津");
        cityMap.put("1101", "市辖区");
        cityMap.put("1201", "市辖区");
        countyMap.put("110101", "通州区");
        countyMap.put("110102", "丰台区");
        countyMap.put("120101", "东丽区");
        countyMap.put("120102", "武清区");
        Map<String, String> targetCityMap = new HashMap<>();

        Map<String, String> targetCountyMap = new HashMap<>();
        //市
        provinceMap.forEach((key, value) -> {
            cityMap.forEach((cityKey, cityValue) -> {
                if (key.equals(cityKey.substring(0, 2))) {
                    targetCityMap.put(cityKey, provinceMap.get(key) + cityMap.get(cityKey));
                }
            });
        });
        //省
        targetCityMap.forEach((key, value) -> {
            countyMap.forEach((countyKey, countyValue) -> {
                if (key.equals(countyKey.substring(0, 4))) {
                    targetCountyMap.put(countyKey, targetCityMap.get(key) + countyMap.get(countyKey));
                }
            });
        });
        System.out.println("targetProvinceMap: " + provinceMap);
        System.out.println("targetCityMap: " + targetCityMap);
        System.out.println("targetCountyMap: " + targetCountyMap);
    }

    public  void test02(String[] args) {
        Map<String, String> provinceMap = new HashMap<>();
        Map<String, String> cityMap = new HashMap<>();
        Map<String, String> countyMap = new HashMap<>();
        provinceMap.put("11", "北京");
        provinceMap.put("12", "天津");
        cityMap.put("1101", "市辖区");
        cityMap.put("1201", "市辖区");
        countyMap.put("110101", "通州区");
        countyMap.put("110102", "丰台区");
        countyMap.put("120101", "东丽区");
        countyMap.put("120102", "武清区");

        // 使用Stream流将原始的provinceMap转换成targetProvinceMap
        Map<String, String> targetProvinceMap = provinceMap.entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

        // 使用Stream流将原始的cityMap转换成targetCityMap
        Map<String, String> targetCityMap = cityMap.entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                        entry -> provinceMap.get(entry.getKey().substring(0, 2))  + entry.getValue()));

        // 使用Stream流将原始的countyMap转换成targetCountyMap
        Map<String, String> targetCountyMap = countyMap.entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                        entry -> targetCityMap.get(entry.getKey().substring(0, 4)) + entry.getValue()));

        // 打印新的三个Map
        System.out.println("targetProvinceMap: " + targetProvinceMap);
        System.out.println("targetCityMap: " + targetCityMap);
        System.out.println("targetCountyMap: " + targetCountyMap);
    }


    public static void main(String[] args) throws JsonProcessingException {
        Map<String, String> provinceMap = new HashMap<>();
        Map<String, String> cityMap = new HashMap<>();
        Map<String, String> countyMap = new HashMap<>();
        List<Map<String,String>> list = new ArrayList<>();
        provinceMap.put("11", "北京");
        provinceMap.put("12", "天津");
        cityMap.put("1101", "市辖区");
        cityMap.put("1201", "市辖区");
        countyMap.put("110101", "通州区");
        countyMap.put("110102", "丰台区");
        countyMap.put("120101", "东丽区");
        countyMap.put("120102", "武清区");


        // 使用Stream流将原始的cityMap转换成targetCityMap并排序
        Map<String, String> targetCityMap = cityMap.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(Map.Entry::getKey,
                        entry -> provinceMap.get(entry.getKey().substring(0, 2))  + entry.getValue(),
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));

        // 使用Stream流将原始的countyMap转换成targetCountyMap并排序
        Map<String, String> targetCountyMap = countyMap.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(Map.Entry::getKey,
                        entry -> targetCityMap.get(entry.getKey().substring(0, 4)) + entry.getValue(),
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        list.add(targetCityMap);
        list.add(targetCountyMap);
//        ObjectMapper objectMapper = new ObjectMapper();
//        String writeValueAsString = objectMapper.writeValueAsString(list);
        System.out.printf("", list.toString());
        String md5 = MD5Utils.toMD5(list.toString());

        Map<String, String> provinceMap2 = new HashMap<>();
        Map<String, String> cityMap2 = new HashMap<>();
        Map<String, String> countyMap2 = new HashMap<>();
        List<Map<String,String>> list2 = new ArrayList<>();
        provinceMap2.put("11", "北京");
        provinceMap2.put("12", "天津");
        cityMap2.put("1101", "市辖区");
        cityMap2.put("1201", "市辖区");
        countyMap2.put("110101", "通州区");
        countyMap2.put("110102", "丰台区");
        countyMap2.put("120101", "东丽区");
        countyMap2.put("120102", "武清区");


        // 使用Stream流将原始的cityMap转换成targetCityMap并排序
        Map<String, String> targetCityMap2 = cityMap2.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(Map.Entry::getKey,
                        entry -> provinceMap2.get(entry.getKey().substring(0, 2))  + entry.getValue(),
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));

        // 使用Stream流将原始的countyMap转换成targetCountyMap并排序
        Map<String, String> targetCountyMap2 = countyMap2.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(Map.Entry::getKey,
                        entry -> targetCityMap2.get(entry.getKey().substring(0, 4)) + entry.getValue(),
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        list2.add(targetCityMap2);
        list2.add(targetCountyMap2);
//        ObjectMapper objectMapper2 = new ObjectMapper();
//        String writeValueAsString2 = objectMapper2.writeValueAsString(list);
        System.out.printf("", list2.toString());
        String md52 = MD5Utils.toMD5(list2.toString());
        boolean equals = md52.equals(md5);
        System.out.printf("*************************",equals );
    }

//    @GetMapping("/download/{filename}")
//    public  void  downloadFile(@PathVariable  String  filename)  throws  IOException  {
//        Resource resource  =  resourceLoader.getResource("file:"  +  filename);
//        if  (resource.exists())  {
//            InputStream  inputStream  =  resource.getInputStream();
//            //  设置响应头，指定文件类型
//            HttpServletResponse  response  =  getResponse();
//            response.setContentType("application/octet-stream");
//            response.setHeader("Content-Disposition",  "attachment;filename="  +  URLEncoder.encode(filename,  "UTF-8"));
//            //  读取文件内容并写入输出流
//            byte[]  buffer  =  new  byte[1024];
//            int  bytesRead;
//            while  ((bytesRead  =  inputStream.read(buffer))  !=  -1)  {
//                response.getOutputStream().write(buffer,  0,  bytesRead);
//            }
//            //  关闭输入输出流
//            inputStream.close();
//            response.getOutputStream().close();
//        }  else  {
//            System.out.println("文件不存在");
//        }
//    }


}
